/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ep.persistence.controllers;

import ep.persistence.controllers.exceptions.IllegalOrphanException;
import ep.persistence.controllers.exceptions.NonexistentEntityException;
import ep.persistence.entities.T006Prescription;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ep.persistence.entities.T012Diagnosis;
import ep.persistence.entities.T005Patient;
import ep.persistence.entities.T003Pharmacist;
import ep.persistence.entities.T002Doctor;
import ep.persistence.entities.T008Dosage;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Spyros
 */
public class T006PrescriptionJpaController {

    public T006PrescriptionJpaController() {
        emf = Persistence.createEntityManagerFactory("EprescPersistencePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(T006Prescription t006Prescription) {
        if (t006Prescription.getT008DosageCollection() == null) {
            t006Prescription.setT008DosageCollection(new ArrayList<T008Dosage>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T012Diagnosis t012Diagnosis = t006Prescription.getT012Diagnosis();
            if (t012Diagnosis != null) {
                t012Diagnosis = em.getReference(t012Diagnosis.getClass(), t012Diagnosis.getId());
                t006Prescription.setT012Diagnosis(t012Diagnosis);
            }
            T005Patient t005Patient = t006Prescription.getT005Patient();
            if (t005Patient != null) {
                t005Patient = em.getReference(t005Patient.getClass(), t005Patient.getId());
                t006Prescription.setT005Patient(t005Patient);
            }
            T003Pharmacist t003Pharmacist = t006Prescription.getT003Pharmacist();
            if (t003Pharmacist != null) {
                t003Pharmacist = em.getReference(t003Pharmacist.getClass(), t003Pharmacist.getId());
                t006Prescription.setT003Pharmacist(t003Pharmacist);
            }
            T002Doctor t002Doctor = t006Prescription.getT002Doctor();
            if (t002Doctor != null) {
                t002Doctor = em.getReference(t002Doctor.getClass(), t002Doctor.getId());
                t006Prescription.setT002Doctor(t002Doctor);
            }
            Collection<T008Dosage> attachedT008DosageCollection = new ArrayList<T008Dosage>();
            for (T008Dosage t008DosageCollectionT008DosageToAttach : t006Prescription.getT008DosageCollection()) {
                t008DosageCollectionT008DosageToAttach = em.getReference(t008DosageCollectionT008DosageToAttach.getClass(), t008DosageCollectionT008DosageToAttach.getId());
                attachedT008DosageCollection.add(t008DosageCollectionT008DosageToAttach);
            }
            t006Prescription.setT008DosageCollection(attachedT008DosageCollection);
            em.persist(t006Prescription);
            if (t012Diagnosis != null) {
                t012Diagnosis.getT006PrescriptionCollection().add(t006Prescription);
                t012Diagnosis = em.merge(t012Diagnosis);
            }
            if (t005Patient != null) {
                t005Patient.getT006PrescriptionCollection().add(t006Prescription);
                t005Patient = em.merge(t005Patient);
            }
            if (t003Pharmacist != null) {
                t003Pharmacist.getT006PrescriptionCollection().add(t006Prescription);
                t003Pharmacist = em.merge(t003Pharmacist);
            }
            if (t002Doctor != null) {
                t002Doctor.getT006PrescriptionCollection().add(t006Prescription);
                t002Doctor = em.merge(t002Doctor);
            }
            for (T008Dosage t008DosageCollectionT008Dosage : t006Prescription.getT008DosageCollection()) {
                T006Prescription oldT006PrescriptionOfT008DosageCollectionT008Dosage = t008DosageCollectionT008Dosage.getT006Prescription();
                t008DosageCollectionT008Dosage.setT006Prescription(t006Prescription);
                t008DosageCollectionT008Dosage = em.merge(t008DosageCollectionT008Dosage);
                if (oldT006PrescriptionOfT008DosageCollectionT008Dosage != null) {
                    oldT006PrescriptionOfT008DosageCollectionT008Dosage.getT008DosageCollection().remove(t008DosageCollectionT008Dosage);
                    oldT006PrescriptionOfT008DosageCollectionT008Dosage = em.merge(oldT006PrescriptionOfT008DosageCollectionT008Dosage);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(T006Prescription t006Prescription) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T006Prescription persistentT006Prescription = em.find(T006Prescription.class, t006Prescription.getId());
            T012Diagnosis t012DiagnosisOld = persistentT006Prescription.getT012Diagnosis();
            T012Diagnosis t012DiagnosisNew = t006Prescription.getT012Diagnosis();
            T005Patient t005PatientOld = persistentT006Prescription.getT005Patient();
            T005Patient t005PatientNew = t006Prescription.getT005Patient();
            T003Pharmacist t003PharmacistOld = persistentT006Prescription.getT003Pharmacist();
            T003Pharmacist t003PharmacistNew = t006Prescription.getT003Pharmacist();
            T002Doctor t002DoctorOld = persistentT006Prescription.getT002Doctor();
            T002Doctor t002DoctorNew = t006Prescription.getT002Doctor();
            Collection<T008Dosage> t008DosageCollectionOld = persistentT006Prescription.getT008DosageCollection();
            Collection<T008Dosage> t008DosageCollectionNew = t006Prescription.getT008DosageCollection();
            List<String> illegalOrphanMessages = null;
            for (T008Dosage t008DosageCollectionOldT008Dosage : t008DosageCollectionOld) {
                if (!t008DosageCollectionNew.contains(t008DosageCollectionOldT008Dosage)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain T008Dosage " + t008DosageCollectionOldT008Dosage + " since its t006Prescription field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (t012DiagnosisNew != null) {
                t012DiagnosisNew = em.getReference(t012DiagnosisNew.getClass(), t012DiagnosisNew.getId());
                t006Prescription.setT012Diagnosis(t012DiagnosisNew);
            }
            if (t005PatientNew != null) {
                t005PatientNew = em.getReference(t005PatientNew.getClass(), t005PatientNew.getId());
                t006Prescription.setT005Patient(t005PatientNew);
            }
            if (t003PharmacistNew != null) {
                t003PharmacistNew = em.getReference(t003PharmacistNew.getClass(), t003PharmacistNew.getId());
                t006Prescription.setT003Pharmacist(t003PharmacistNew);
            }
            if (t002DoctorNew != null) {
                t002DoctorNew = em.getReference(t002DoctorNew.getClass(), t002DoctorNew.getId());
                t006Prescription.setT002Doctor(t002DoctorNew);
            }
            Collection<T008Dosage> attachedT008DosageCollectionNew = new ArrayList<T008Dosage>();
            for (T008Dosage t008DosageCollectionNewT008DosageToAttach : t008DosageCollectionNew) {
                t008DosageCollectionNewT008DosageToAttach = em.getReference(t008DosageCollectionNewT008DosageToAttach.getClass(), t008DosageCollectionNewT008DosageToAttach.getId());
                attachedT008DosageCollectionNew.add(t008DosageCollectionNewT008DosageToAttach);
            }
            t008DosageCollectionNew = attachedT008DosageCollectionNew;
            t006Prescription.setT008DosageCollection(t008DosageCollectionNew);
            t006Prescription = em.merge(t006Prescription);
            if (t012DiagnosisOld != null && !t012DiagnosisOld.equals(t012DiagnosisNew)) {
                t012DiagnosisOld.getT006PrescriptionCollection().remove(t006Prescription);
                t012DiagnosisOld = em.merge(t012DiagnosisOld);
            }
            if (t012DiagnosisNew != null && !t012DiagnosisNew.equals(t012DiagnosisOld)) {
                t012DiagnosisNew.getT006PrescriptionCollection().add(t006Prescription);
                t012DiagnosisNew = em.merge(t012DiagnosisNew);
            }
            if (t005PatientOld != null && !t005PatientOld.equals(t005PatientNew)) {
                t005PatientOld.getT006PrescriptionCollection().remove(t006Prescription);
                t005PatientOld = em.merge(t005PatientOld);
            }
            if (t005PatientNew != null && !t005PatientNew.equals(t005PatientOld)) {
                t005PatientNew.getT006PrescriptionCollection().add(t006Prescription);
                t005PatientNew = em.merge(t005PatientNew);
            }
            if (t003PharmacistOld != null && !t003PharmacistOld.equals(t003PharmacistNew)) {
                t003PharmacistOld.getT006PrescriptionCollection().remove(t006Prescription);
                t003PharmacistOld = em.merge(t003PharmacistOld);
            }
            if (t003PharmacistNew != null && !t003PharmacistNew.equals(t003PharmacistOld)) {
                t003PharmacistNew.getT006PrescriptionCollection().add(t006Prescription);
                t003PharmacistNew = em.merge(t003PharmacistNew);
            }
            if (t002DoctorOld != null && !t002DoctorOld.equals(t002DoctorNew)) {
                t002DoctorOld.getT006PrescriptionCollection().remove(t006Prescription);
                t002DoctorOld = em.merge(t002DoctorOld);
            }
            if (t002DoctorNew != null && !t002DoctorNew.equals(t002DoctorOld)) {
                t002DoctorNew.getT006PrescriptionCollection().add(t006Prescription);
                t002DoctorNew = em.merge(t002DoctorNew);
            }
            for (T008Dosage t008DosageCollectionNewT008Dosage : t008DosageCollectionNew) {
                if (!t008DosageCollectionOld.contains(t008DosageCollectionNewT008Dosage)) {
                    T006Prescription oldT006PrescriptionOfT008DosageCollectionNewT008Dosage = t008DosageCollectionNewT008Dosage.getT006Prescription();
                    t008DosageCollectionNewT008Dosage.setT006Prescription(t006Prescription);
                    t008DosageCollectionNewT008Dosage = em.merge(t008DosageCollectionNewT008Dosage);
                    if (oldT006PrescriptionOfT008DosageCollectionNewT008Dosage != null && !oldT006PrescriptionOfT008DosageCollectionNewT008Dosage.equals(t006Prescription)) {
                        oldT006PrescriptionOfT008DosageCollectionNewT008Dosage.getT008DosageCollection().remove(t008DosageCollectionNewT008Dosage);
                        oldT006PrescriptionOfT008DosageCollectionNewT008Dosage = em.merge(oldT006PrescriptionOfT008DosageCollectionNewT008Dosage);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = t006Prescription.getId();
                if (findT006Prescription(id) == null) {
                    throw new NonexistentEntityException("The t006Prescription with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T006Prescription t006Prescription;
            try {
                t006Prescription = em.getReference(T006Prescription.class, id);
                t006Prescription.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The t006Prescription with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<T008Dosage> t008DosageCollectionOrphanCheck = t006Prescription.getT008DosageCollection();
            for (T008Dosage t008DosageCollectionOrphanCheckT008Dosage : t008DosageCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This T006Prescription (" + t006Prescription + ") cannot be destroyed since the T008Dosage " + t008DosageCollectionOrphanCheckT008Dosage + " in its t008DosageCollection field has a non-nullable t006Prescription field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            T012Diagnosis t012Diagnosis = t006Prescription.getT012Diagnosis();
            if (t012Diagnosis != null) {
                t012Diagnosis.getT006PrescriptionCollection().remove(t006Prescription);
                t012Diagnosis = em.merge(t012Diagnosis);
            }
            T005Patient t005Patient = t006Prescription.getT005Patient();
            if (t005Patient != null) {
                t005Patient.getT006PrescriptionCollection().remove(t006Prescription);
                t005Patient = em.merge(t005Patient);
            }
            T003Pharmacist t003Pharmacist = t006Prescription.getT003Pharmacist();
            if (t003Pharmacist != null) {
                t003Pharmacist.getT006PrescriptionCollection().remove(t006Prescription);
                t003Pharmacist = em.merge(t003Pharmacist);
            }
            T002Doctor t002Doctor = t006Prescription.getT002Doctor();
            if (t002Doctor != null) {
                t002Doctor.getT006PrescriptionCollection().remove(t006Prescription);
                t002Doctor = em.merge(t002Doctor);
            }
            em.remove(t006Prescription);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<T006Prescription> findT006PrescriptionEntities() {
        return findT006PrescriptionEntities(true, -1, -1);
    }

    public List<T006Prescription> findT006PrescriptionEntities(int maxResults, int firstResult) {
        return findT006PrescriptionEntities(false, maxResults, firstResult);
    }

    private List<T006Prescription> findT006PrescriptionEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(T006Prescription.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public T006Prescription findT006Prescription(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(T006Prescription.class, id);
        } finally {
            em.close();
        }
    }

    public int getT006PrescriptionCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<T006Prescription> rt = cq.from(T006Prescription.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
